<?php
//error_reporting(0);

if(!isset($_POST) || !isset($_GET['act'])){
	exit;
}

include_once("./cfg.php");

@$mysqli = new mysqli($config['host'],$config['root'],$config['pass'],$config['dbname'],$config['port']);

if($mysqli->connect_errno){
	exit("数据库连接错误" . $mysqli->connect_error);
}

session_start();

$type = trim($_GET['act']);

switch($type){
	case 'reg':	//注册请求
		$userName = trim($_POST['userName']);
		$userPass = md5(trim($_POST['userPass']));
		$userPass1 = md5(trim($_POST['userPass1']));
		$qd = trim($_POST['qd']);
		if(strlen($userName) < 1){
			exit(json_encode(array("code"=>0,"msg"=>"请填写正确的账号")));
		}
		if(strlen($userPass) < 1){
			exit(json_encode(array("code"=>0,"msg"=>"请填写正确的密码")));
		}
		if($userPass != $userPass1){
			exit(json_encode(array("code"=>0,"msg"=>"两次密码不一致")));
		}
		$sql = "SELECT * FROM `zhuce`.`zhanghao` WHERE `userName`='{$userName}'";
		$row = $mysqli->query($sql);
		if($row === false){
			exit(json_encode(array("code"=>0,"msg"=>$mysqli->error)));
		}
		if($row->num_rows > 0){
			exit(json_encode(array("code"=>0,"msg"=>"该账号已被使用")));
		}
		
		$token = md5($userName . time() . $key);
		$sql = "INSERT INTO `zhuce`.`zhanghao` (`userName`, `userPass`) VALUES ('{$userName}', '{$userPass}')";
		$row = $mysqli->query($sql);
		if($row === false){
			exit(json_encode(array("code"=>0,"msg"=>$mysqli->error)));
		}
		if($mysqli->errno){
			exit($mysqli->error);
		}
		/*$ret = array(
			"code"=>1,
			"msg"=>"success",
			"pfId"=>"mkhf",
			"gameId"=>102,
			"passId"=>$data['id'],
			"openId"=>$token,
			"nonce"=>"xW0IR4xx",
			"ts"=>time(),
			"serverUrl"=>"",
		);
		$_SESSION['userName'] = $userName;
		$_SESSION['userPass'] = $userPass;
		$_SESSION['token'] = $token;
		exit(json_encode($ret));
		break;*/
		$type = "login";
	case "login":
		$userName = trim($_POST['userName']);
		$userPass = md5(trim($_POST['userPass']));
		if(strlen($userName) < 1){
			exit(json_encode(array("code"=>2,"msg"=>"请填写正确的账号","data"=>"no"),JSON_UNESCAPED_UNICODE));
		}
		if(strlen($userPass) < 1){
			exit(json_encode(array("code"=>2,"msg"=>"请填写正确的密码","data"=>"no"),JSON_UNESCAPED_UNICODE));
		}
		$sql = "SELECT * FROM `zhuce`.`zhanghao` WHERE `userName`='{$userName}' LIMIT 1";
		$row = $mysqli->query($sql);
		if($row === false){
			exit(json_encode(array("code"=>2,"msg"=>$mysqli->error),JSON_UNESCAPED_UNICODE));
		}
		if($row->num_rows < 1){
			exit(json_encode(array("code"=>2,"msg"=>"账号不存在","data"=>"no"),JSON_UNESCAPED_UNICODE));	
		}
		$data = $row->fetch_array(1);
		if($data['userPass'] != $userPass){
			exit(json_encode(array("code"=>1,"msg"=>"密码错误","data"=>"no"),JSON_UNESCAPED_UNICODE));
		}
		$ret = array(
			"code"=>0,
			"msg"=>"ok",
			"data"=>["userName"=>$userName]
		);
		$_SESSION = $ret;
		unset($_SESSION['code']);
		unset($_SESSION['msg']);
		exit(json_encode($ret));
		break;
}













?>